﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml;
using Tzw.Common;

namespace Dafybjy
{
    public class HisService
    {

        string _jsonFile = "appsettings.json";
        string _key = "6F1382635EEB9135";                           //秘钥
        private string _token = "858615652062B97E4211A075EBE83E8A"; //his提供的token
        string _transToken;                                         //传输用的token
        private string _jslb = "金泰格自助机";                            //结算类别
        string _apiUrl = "http://10.10.100.230:8086/EXTERNALSERVICES/ZL_INFORMATIONSERVICE.ASMX/USERMANAGER";

        public HisService()
        {
            //初始化配置信息
            _key = ConfigurationManager.AppSettings["key"] ?? "6F1382635EEB9135";
            _token = ConfigurationManager.AppSettings["token"] ?? "858615652062B97E4211A075EBE83E8A";
            var baseUrl = ConfigurationManager.AppSettings["baseUrl"] ?? "http://10.10.100.230:8086";
            _apiUrl = baseUrl + "/ExternalServices/ZL_InformationService.asmx/";

            _transToken = Secret.Encrypt_AES(_token, _key);
        }

        /// <summary>
        /// 1通过健康卡查询病人信息
        /// </summary>
        /// <returns></returns>
        public string GetPatientIdByCardId(string cardId)
        {
            try
            {
                Logger.Default.Debug($"病人信息查询：<ZJKH>{cardId}</ZJKH><KLB>居民健康卡</KLB>");

                //请求内容
                var dataParam = Secret.Encrypt_AES($"<ZJKH>{cardId}</ZJKH><KLB>居民健康卡</KLB>", _key);

                //请求数据拼接
                var body = AggregatePara("BindCard.UserInfoByJZK.Query", _transToken, dataParam);

                //请求Neri
                var apiResult = SoapHelper.GetSoapReSource(_apiUrl + "Custom", body, "application/x-www-form-urlencoded");

                var doc = new XmlDocument();
                if (doc.GetCdData(apiResult, "ROOT/STATE") == "T")
                {
                    //获取密文
                    var secreteContent = doc.GetCdData(apiResult, "ROOT/DATAPARAM");

                    //密文解密
                    var content = Secret.Decrypt_AES(secreteContent, _key);

                    Logger.Default.Debug($"1通过健康卡查询病人信息-密文内容：{content}");

                    var xmlContent = new XmlDocument();
                    return xmlContent.GetCdData($"<ROOT>{content}</ROOT>", "ROOT/BRID");
                }

                Logger.Default.Error($"1通过健康卡查询病人信息接口请求失败：{apiResult}");
                return "";
            }
            catch (Exception e)
            {
                Logger.Default.Error($"查询病人信息异常：{e.Message}{e.StackTrace}");
                return "";
            }

        }


        /// <summary>
        /// 拼装要提交的数据
        /// </summary>
        /// <param name="service">服务器名称</param>
        /// <param name="token">令牌</param>
        /// <param name="dataParam">参数</param>
        /// <returns></returns>
        private string AggregatePara(string service, string token, string dataParam)
        {
            return "ReData=" + HttpUtility.UrlEncode(
                $"<ROOT><TOKEN><![CDATA[{token}]]></TOKEN><SERVICE><![CDATA[{service}]]></SERVICE><DATAPARAM><![CDATA[{dataParam}]]></DATAPARAM></ROOT>");
        }

    }
}
